package org.holynite.fieldmanage.repositories

import org.holynite.fieldmanage.persistence.EAO

import javax.persistence.EntityManager
import javax.persistence.PersistenceContext
import javax.persistence.PersistenceContextType

/**
 * Created by midnite on 15/1/7.
 */
abstract class BaseEAO implements EAO {
	@PersistenceContext
	EntityManager em

	@Override
	def <T> T get(Class<T> entityClass, Object pk) {
		em.find(entityClass, pk) as T
	}

	@Override
	void save(Object entity) {
		em.persist(entity)
	}

	@Override
	void update(Object entity) {
		em.merge(entity)
	}

	@Override
	void delete(Class entityClass, Object pk) {
		em.remove(em.getReference(entityClass, pk))
	}
}
